<?php

namespace common\models\base;
use common\config\AppConf;
use common\libs\Constants;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/23 0023
 * Time: 上午 9:21
 */

class BaseModel extends \yii\db\ActiveRecord
{
	const STATUS_ACTIVE = 1;//yes
	const STATUS_DELETED = 2;//no

	public function behaviors()
	{
		return [
			'timestamp_behavior'=>[
				'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'create_time',
                'updatedAtAttribute' => 'update_time'
			],
		];
	}

	
	/**
	 * 把日期格式的数据转换成时间戳
	 * 使用方式，rules里配置：[['field'],'toTimestamp']
	 * @param $attribute
	 */
	public function toTimestamp($attribute){
		if(is_string($this->$attribute)){
			$this->$attribute = strtotime($this->$attribute);
		}
	}


	
	/**
	 * User: wangxiaoxiao
	 * Description: 获取当日日期
	 */
    public function getDate()
    {
        return date('Ymd',time());
	}
	
	/**
	 * 查询两个时间段是否交叉的语句，Unix时间戳方式
	 * @param $startTime
	 * @param $endTime
	 * @param string $start_time
	 * @param string $end_time
	 * @return array
	 */
	public function getSelectTimeUnix($startTime, $endTime,$start_time='start_time',$end_time='end_time')
	{
		$betweenWhere = [];
		if ($startTime && $endTime) {
			$betweenWhere1 =['and',['>=',$start_time,$startTime],['<=',$end_time,$endTime]];
			$betweenWhere2 =['and',['<=',$start_time,$startTime],['<=',$end_time,$endTime],['>=',$end_time,$startTime]];
			$betweenWhere3 =['and',['>=',$start_time,$startTime],['>=',$end_time,$endTime],['<=',$start_time,$endTime]];
			$betweenWhere4 =['and',['<=',$start_time,$startTime],['>=',$end_time,$endTime]];
			$betweenWhere= ['or',$betweenWhere1,$betweenWhere2,$betweenWhere3,$betweenWhere4];
		}
		return $betweenWhere;
	}
	
	/**
	 * 返回数据
	 * @param $data
	 * @param int $no
	 * @param string $msg
	 * @param bool $r
	 * @return array
	 */
	public static function renderArray($data, $no = AppConf::APP_NO_OK, $msg = AppConf::APP_MSG_OK, $r = true)
	{
		return ['r' => $r, 'no' => $no, 'data' => $data, 'msg' => $msg];
	}
	
	public static function renderError($msg = '服务器内部错误', $no = AppConf::APP_NO_DB)
	{
		return self::renderArray([], $no, $msg, false);
	}
	
	public static function renderSuccess()
	{
		return self::renderArray([]);
	}
}